﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using Water.Model;
using WaterApp.Common;

namespace WaterApp.Business
{
    public partial class SmallValveOperate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            if (hfmrno.Value == "")
            {
                hfmrno.Value = BuilderMrno.GetMrnoFortbl("Tbl_SmallValveOperatePlan");
            }
            string url = WebapiUrl.weburl() + "AddSmallValveOperatePlan";
            SmallValveOperatePlan svop = new SmallValveOperatePlan()
            {
                MRNO = hfmrno.Value,
                FillDt = Convert.ToDateTime(tbfilldt.Text),
                StopDt = Convert.ToDateTime(tbdshdt.Text),
                Address = tbaddress.Text,
                WorkDt = Convert.ToDateTime(tbshgdt.Text),
                WorkNr = tbgznrong.Text,
                WorkNo = "",
                ValveID = tbfmno.Text,
                StopYdDt = Convert.ToDateTime(tbydclosedt.Text),
                StartYdDt = Convert.ToDateTime(tbydstartdt.Text),
                BeforeOperZb = GetCheckBoxListSelValue(cblbeforezb), //cblbeforezb.SelectedValue,
                PingGyx = tbpinggu.Text,
                AfterOper = GetCheckBoxListSelValue(cblafteroper), //cblafteroper.SelectedValue,
                OperImage = "",
                OperEmp = Membership.GetUser().UserName
            };
            var requestJson = JsonConvert.SerializeObject(svop);
            HttpContent httpContent = new StringContent(requestJson);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync(url, httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            if (responseJson == "true")
            {
                lbmessage.Text = "保存成功!";
            }
            else
            {
                lbmessage.Text = "保存时发生异常！";
            }
        }

        protected void lnkDel_Click(object sender, EventArgs e)
        {
            string url = WebapiUrl.weburl() + "DeleteSmallValveOperatePlan/" + hfmrno.Value + "/" + tbfmno.Text;            
            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            if (responseJson == "true")
            {
                lbmessage.Text = "删除成功!";
            }
            else
            {
                lbmessage.Text = "删除时发生异常！";
            }
        }

        private string GetCheckBoxListSelValue(CheckBoxList cbl)
        {
            StringBuilder strbul = new StringBuilder();
            foreach (ListItem li in cbl.Items)
            {
                if (li.Selected)
                {
                    strbul.Append(li.Value+"|");
                }
            }
            return strbul.ToString().Substring(0,strbul.ToString().Length-1);
        }

        protected void lnkQuery_Click(object sender, EventArgs e)
        {
            string url = WebapiUrl.weburl() + "GetSmallValveOperatePlanQuery/"+tbmrno.Text;
            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            SmallValveOperatePlanQuery svopq = JsonConvert.DeserializeObject<SmallValveOperatePlanQuery>(responseJson);
            if (svopq.MRNO != null)
            {
                hfmrno.Value = svopq.MRNO;
                tbfilldt.Text = svopq.FillDt.ToString();
                tbdshdt.Text = svopq.StopDt.ToString();
                tbaddress.Text = svopq.Address;
                tbshgdt.Text = svopq.WorkDt.ToString();
                tbgznrong.Text = svopq.WorkNr;
                //WorkNo = "",
                tbfmno.Text = svopq.ValveID;
                tbydclosedt.Text = svopq.StopYdDt.ToString();
                tbshjclosedt.Text = svopq.CloseDt.ToString();
                tbydstartdt.Text = svopq.StartYdDt.ToString();
                tbshjstartdt.Text = svopq.StartDt.ToString();
                tbzhuanshu.Text = svopq.ZhuanCount.ToString();
                tbkqfangx.Text = svopq.StartDirection;
                //cblbeforezb.Text = svopq.FillDt.ToString();
                SetCheckBoxList(cblbeforezb, svopq.BeforeOperZb);
                tbpinggu.Text = svopq.PingGyx;
                SetCheckBoxList(cblafteroper, svopq.AfterOper);                
                //    OperImage = "",
                //    OperEmp = Membership.GetUser().UserName
            }
        }

        private void SetCheckBoxList(CheckBoxList cbl, string selvalue)
        {
            string[] selv = selvalue.Split('|');
            for (int n = 0; n < selv.Length; n++)
            {
                foreach (ListItem li in cbl.Items)
                {
                    if (li.Value == selv[n])
                    {
                        li.Selected = true;
                    }
                }
            }
        }
    }
}